/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.Lab;
import me.zhengjie.modules.biz.domain.ResidualAirVolume;
import me.zhengjie.modules.biz.repository.LabRepository;
import me.zhengjie.modules.biz.service.dto.FumeHoodDto;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.ResidualAirVolumeRepository;
import me.zhengjie.modules.biz.service.ResidualAirVolumeService;
import me.zhengjie.modules.biz.service.dto.ResidualAirVolumeDto;
import me.zhengjie.modules.biz.service.dto.ResidualAirVolumeQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.ResidualAirVolumeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-02-17
**/
@Service
@RequiredArgsConstructor
public class ResidualAirVolumeServiceImpl implements ResidualAirVolumeService {

    private final ResidualAirVolumeRepository residualAirVolumeRepository;
    private final ResidualAirVolumeMapper residualAirVolumeMapper;
    private final LabRepository labRepository;

    @Override
    public Map<String,Object> queryAll(ResidualAirVolumeQueryCriteria criteria, Pageable pageable){
        Page<ResidualAirVolume> page = residualAirVolumeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Page<ResidualAirVolumeDto> dto = page.map(residualAirVolumeMapper::toDto);
        List<ResidualAirVolumeDto> resultDto = dto.getContent();
        for (ResidualAirVolumeDto residualAirVolumeDto:resultDto) {
            Lab lab = labRepository.findById(residualAirVolumeDto.getLabId()).orElseGet(Lab::new);
            residualAirVolumeDto.setLabName(lab.getName());
        }
        return PageUtil.toPage(dto);
    }

    @Override
    public List<ResidualAirVolumeDto> queryAll(ResidualAirVolumeQueryCriteria criteria){
        List<ResidualAirVolume> residualAirVolumeList = residualAirVolumeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
        List<ResidualAirVolumeDto> residualAirVolumeDtoList = residualAirVolumeMapper.toDto(residualAirVolumeList);
        for (ResidualAirVolumeDto residualAirVolumeDto : residualAirVolumeDtoList) {
            Lab lab = labRepository.findById(residualAirVolumeDto.getLabId()).orElseGet(Lab::new);
            residualAirVolumeDto.setLabName(lab.getName());
        }
        return residualAirVolumeDtoList;
    }

    @Override
    @Transactional
    public ResidualAirVolumeDto findById(Integer id) {
        ResidualAirVolume residualAirVolume = residualAirVolumeRepository.findById(id).orElseGet(ResidualAirVolume::new);
        ValidationUtil.isNull(residualAirVolume.getId(),"ResidualAirVolume","id",id);
        return residualAirVolumeMapper.toDto(residualAirVolume);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResidualAirVolumeDto create(ResidualAirVolume resources) {
        return residualAirVolumeMapper.toDto(residualAirVolumeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ResidualAirVolume resources) {
        ResidualAirVolume residualAirVolume = residualAirVolumeRepository.findById(resources.getId()).orElseGet(ResidualAirVolume::new);
        ValidationUtil.isNull( residualAirVolume.getId(),"ResidualAirVolume","id",resources.getId());
        residualAirVolume.copy(resources);
        residualAirVolumeRepository.save(residualAirVolume);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            residualAirVolumeRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ResidualAirVolumeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ResidualAirVolumeDto residualAirVolume : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", residualAirVolume.getName());
            map.put("余风量ip", residualAirVolume.getIp());
            map.put("地址", residualAirVolume.getAddress());
            map.put("实验室", residualAirVolume.getLabName());
            map.put("风量差量", residualAirVolume.getAirVolumeDifference());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
